package com.fragment;

import java.util.ArrayList;

import com.example.lizhiinformation.R;
import com.myadapter.Fragementadapter;
import com.tools.Path_URL;

import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class Fragment_ViewPager extends Fragment {
	protected static final String RadioButton = null;

	private ViewPager viewpager;
	private RadioGroup radiogroup;
	private HorizontalScrollView scrollview;
	private FrontPage fragement1;
	private FragmentAll jiangsu;
	private FragmentAll guonei;
	private FragmentAll guoji;
	private FragmentAll shehui;
	private FragmentAll junshi;
	private FragmentAll yule;
	private FragmentAll tiyu;
	private FragmentAll keji;
	private FragmentAll guojizuqiu;
	private FragmentNoTop lizhipai;
	private FragmentNoTop dujiayifan;
	private Fragment_Zhengxiangdi zhengxiangdi;
	private FragmentNoTop toubansudi;
	private FragmentNoTop pangziyuping;
	private ArrayList<Fragment> list;
	private Fragementadapter adapter;
	private TextView indicateid;
	private LinearLayout.LayoutParams params;
	private Animation animation;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view2 = inflater.inflate(R.layout.fragment_viewpager, container,
				false);

		return view2;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);
		viewpager = (ViewPager) view.findViewById(R.id.Fragement_viewpage);
		radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup);
		scrollview = (HorizontalScrollView) view.findViewById(R.id.scrollview);
		indicateid = (TextView) view.findViewById(R.id.indicateId);
		params = (android.widget.LinearLayout.LayoutParams) indicateid
				.getLayoutParams();
		animation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		animation.setDuration(700);
		animation.setFillAfter(true);
		animation.setFillBefore(false);
		RadioButton radiaobutton1 = (android.widget.RadioButton) view.findViewById(R.id.radiaobutton1);
		if(radiaobutton1.isChecked()){
			radiaobutton1.startAnimation(animation);
		}else{
			radiaobutton1.clearAnimation();
		}
		findview();
		fram();
	}

	/**
	 * viewpager设置
	 */
	public void fram() {
		// viewpager和radiobutton关联
		viewpager.setOnPageChangeListener(new OnPageChangeListener() {



			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				RadioButton childAt = (RadioButton) radiogroup.getChildAt(arg0);
				childAt.setChecked(true);
				// scrollview的跳转
				for(int i=0;i<15;i++){
					RadioButton ridaobutton = (RadioButton) radiogroup.getChildAt(i);
					if(ridaobutton.isChecked()){
						ridaobutton.startAnimation(animation);
					}else{
						ridaobutton.clearAnimation();
					}
				}
				int left = childAt.getLeft();
				int right = childAt.getRight();
				int width = getActivity().getWindow().getDecorView().getWidth();
				scrollview.scrollTo(left - width / 2 + (right - left) / 2, 0);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// 动画和指示条
				TranslateAnimation animation = new TranslateAnimation(
						params.width * arg0, 0, 0, 0);
				animation.setDuration(0);
				indicateid.startAnimation(animation);
				if (arg1 == 0) {
					params.setMargins((params.width * arg0), 0, 0, 0);
				} else {
					params.setMargins((int) (params.width * (arg0 + arg1)), 0,
							0, 0);
				}
				indicateid.setLayoutParams(params);
				indicateid.clearAnimation();

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		// 点击radiobutton跳转到指定的viewpager
		for (int i = 0; i < 15; i++) {
			RadioButton radiobutton = (RadioButton) radiogroup.getChildAt(i);
			radiobutton.setTag(i);
			radiobutton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					viewpager.setCurrentItem((Integer) arg0.getTag());
				}
			});
		}
		viewpager.setAdapter(adapter);

	}

	public void findview() {

		fragement1 = new FrontPage();

		jiangsu = new FragmentAll(Path_URL.JIANGSU_URL1, Path_URL.JIANGSU_URL2,
				Path_URL.JIANGSU_URLTOP, Path_URL.JIANGSU_URLITEMS1,
				Path_URL.JIANGSU_URLITEMS2);
		guonei = new FragmentAll(Path_URL.GUONAI, Path_URL.GUONAI2,
				Path_URL.GUONAI_TOP, Path_URL.GUONAI_ITEMS,
				Path_URL.guoji_items2);
		guoji = new FragmentAll(Path_URL.guoji1, Path_URL.guoji2,
				Path_URL.guoji_top, Path_URL.guoji_items, Path_URL.guoji_items2);
		shehui = new FragmentAll(Path_URL.shehui1, Path_URL.shehui2,
				Path_URL.shehui_top, Path_URL.shehui_items,
				Path_URL.shehui_items2);
		junshi = new FragmentAll(Path_URL.junshi1, Path_URL.junshi2,
				Path_URL.junshi_top, Path_URL.junshi_items,
				Path_URL.junshi_items2);
		yule = new FragmentAll(Path_URL.yule1, Path_URL.yule2,
				Path_URL.yule_top, Path_URL.yule_items, Path_URL.yule_items2);
		tiyu = new FragmentAll(Path_URL.tiyu1, Path_URL.tiyu2,
				Path_URL.tiyu_top, Path_URL.tiyu_items, Path_URL.tiyu_items2);
		keji = new FragmentAll(Path_URL.keji1, Path_URL.keji2,
				Path_URL.keji_top, Path_URL.keji_items, Path_URL.keji_items2);
		guojizuqiu = new FragmentAll(Path_URL.guojizuqiu1,
				Path_URL.guojizuqiu2, Path_URL.guojizuqiui_top,
				Path_URL.guojizuqiu_items, Path_URL.guojizuqiu_items2);
		lizhipai = new FragmentNoTop(Path_URL.LIZHIPAI, Path_URL.LIZHIPAI2,
				R.drawable.lzp);
		dujiayifan = new FragmentNoTop(Path_URL.DUJIAYIFAN,
				Path_URL.DUJIAYIFAN2, R.drawable.djyf);
		zhengxiangdi = new Fragment_Zhengxiangdi(Path_URL.ZHENGXIANGDI,
				Path_URL.ZHENGXIANGDI2, Path_URL.ZHANGXIANGDI_ITEMS);
		toubansudi = new FragmentNoTop(Path_URL.toubansudi1,
				Path_URL.toubansudi2, R.drawable.dbsd);
		pangziyuping = new FragmentNoTop(Path_URL.pangziyuping1,
				Path_URL.pangziyuping2, R.drawable.pzyp);
		list = new ArrayList<Fragment>();
		list.add(fragement1);
		list.add(jiangsu);
		list.add(lizhipai);
		list.add(dujiayifan);
		list.add(zhengxiangdi);
		list.add(guonei);
		list.add(guoji);
		list.add(shehui);
		list.add(junshi);
		list.add(yule);
		list.add(tiyu);
		list.add(keji);
		list.add(toubansudi);
		list.add(pangziyuping);
		list.add(guojizuqiu);
		adapter = new Fragementadapter(getFragmentManager(), list);
	}

	public void findridaobutton(View view) {

	}

}
